define(["jquery", "easy-admin", "treetable", "iconPickerFa", "autocomplete"], function ($, ea) {

    var table = layui.table,
        treetable = layui.treetable,
        iconPickerFa = layui.iconPickerFa,
        autocomplete = layui.autocomplete;

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'nursinguser.kecheng/index'+parameter,
        add_url: 'nursinguser.kecheng/add'+parameter,
        delete_url: 'nursinguser.kecheng/delete',
        edit_url: 'nursinguser.kecheng/edit',
        modify_url: 'nursinguser.kecheng/modify',
        crud_url:'nursinguser.kecheng/crud',
        updateFile:'nursinguser.kecheng/updateField',
    };

    var Controller = {
        index: function () {

            var renderTable = function () {
                layer.load(2);
                treetable.render({
                    treeColIndex: 2,
                    treeSpid: 0,
                    homdPid: 99999999,
                    treeIdName: 'id',
                    treePidName: 'pid',
                    url: ea.url(init.index_url),
                    elem: init.table_elem,
                    id: init.table_render_id,
                    treeDefaultClose:true,
                    toolbar: '#toolbar',
                    
                    page: false,
                    // skin: 'line',

                    // @todo 不直接使用ea.table.render(); 进行表格初始化, 需要使用 ea.table.formatCols(); 方法格式化`cols`列数据
                    cols: ea.table.formatCols([[
                        {type: 'checkbox'},
                        {field: 'id',title:"ID",minWidth:80},
                                            {field: 'name', title: '课程名称',width:250,search:false,align: 'left'},
                    {field: 'sort', title: '排序',search:false},
                    {field: 'staff_grade_id', title: '等级',search:false},
                    {field: 'type', title: '视频类型',selectList:{"1":"<span style='color: #00CC00'>视频<\/span>","2":"<span style='color: #3399CC'>文字<\/span>"},search:false},
                    {field: 'create_time', title: '创建时间',search:'range'},
                    {field: 'status', title: '状态',  search: false, tips: '开启|关闭', templet: ea.table.switch},

                        {
                        width: 200,
                        title: '操作',
                        templet:"#right_button",
                    }
                    ]], init),
                    done: function () {
                        layer.closeAll('loading');
                    }
                });
            };

            renderTable();

            $('body').on('click', '[data-treetable-refresh]', function () {
                renderTable();
            });

            $('body').on('click', '[data-treetable-delete]', function () {
                var tableId = $(this).attr('data-treetable-delete'),
                    url = $(this).attr('data-url');
                tableId = tableId || init.table_render_id;
                url = url != undefined ? ea.url(url) : window.location.href;
                var checkStatus = table.checkStatus(tableId),
                    data = checkStatus.data;
                if (data.length <= 0) {
                    ea.msg.error('请勾选需要删除的数据');
                    return false;
                }
                var ids = [];
                $.each(data, function (i, v) {
                    ids.push(v.id);
                });
                ea.msg.confirm('确定删除？', function () {
                    ea.request.post({
                        url: url,
                        data: {
                            id: ids
                        },
                    }, function (res) {
                        ea.msg.success(res.msg, function () {
                            renderTable();
                        });
                    });
                });
                return false;
            });

            ea.table.listenSwitch({filter: 'status', url: init.modify_url});
            
            ea.table.listenEdit(init, 'currentTable', init.table_render_id, true);

            ea.listen();
        },
        add: function () {
        var time = 0;
        $(document).on('click','[data-type=tianjia]',function (e){
                var html = "<div class=\"layui-row\">\n" +
                                        "                        <div class=\"layui-form-mid\">键：</div><input class=\"layui-input layui-input-inline\" style=\"width: 100px\" name=\""+field+"[key][]\" value=\"\">\n" +
                                        "                        <div class=\"layui-form-mid\">值：</div><input class=\"layui-input layui-input-inline\" style=\"width: 150px\" name=\""+field+"[value][]\" value=\"\">\n" +
                                        "                        <div class=\"layui-form-mid\"><input type=\"button\" data-type=\"jian\" class=\"layui-btn layui-btn-sm layui-btn-primary layui-border-red\" value=\"-\"></div>\n" +
                                        "                    </div>"
                                    $(this).parent().parent().append(html)
                                })
                                    $(document).on('click','[data-type=jian]',function (e){
                                        console.log(e);
                                        $(this).parent().parent().remove();
                                    })
                    $(".aselect").bind('input propertychange',function (data){
                                            var value = $(this).val();
                                            var url = data.target.dataset.url;
                                            var show_id = data.target.dataset.contentid;
                                            var name = data.target.dataset.name
                                            var table = data.target.dataset.table
                                            var relationship = data.target.dataset.relationship
                                            // console.log(data)
                                            // console.log(show_id)
                                            // console.log(name)
                                            // console.log(table)
                                            // console.log(relationship)
                                            // console.log(url)
                                            // return
                                            time = new Date().getTime();
                                            setTimeout(function (){
                                                var newtime = new Date().getTime();
                                                if(newtime>=time+499){
                                                    $.post(url,{table:table,value:value,name:name,relationship_primary_key:relationship},function (res){
                                                        // console.log(res);
                                                        var select_show = $("#"+show_id);
                                                        select_show.empty();
                                                        if(res.data.length>0){
                                                           var str = '<dd class="xuanzes" data-value="" class="">请选择</dd>';
                                                            $.each(res.data,function (i,v){
                                                                str +='<dd class="xuanzes" data-value="' + v[relationship] + '" class="">' + v[name] + '</dd>';
                                                            })
                                                            select_show.append(str)
                                                            select_show.show();
                                                        }else{
                                                            select_show.hide();
                                                        }
                                                    })
                                                    $('body').on('click','dd',function (data){
                                                        console.log(data);
                                                        var value = data.target.dataset.value;
                                                        var text = data.target.innerText;
                                                        // $(this).parent().prev().children('input')
                                                        // console.log($(this).parent().prev().children('input'))
                                                        $.each($(this).parent().prev().children('input'),function (i,v){
                                                            if(i==1){
                                                                $(v).val(value);
                                                            }else if(i==0){
                                                                $(v).val(text);
                                                            }
                                                        })
                                                        $(this).parent().hide();
                                                    })
                                                }else{
                                                    console.log('时间没到');
                                                }
                                            },500);
                                        })
            iconPickerFa.render({
                elem: '#icon',
                url: PATH_CONFIG.iconLess,
                limit: 12,
                click: function (data) {
                    $('#icon').val('fa ' + data.icon);
                },
                success: function (d) {
                    console.log(d);
                }
            });
            autocomplete.render({
                elem: $('#href')[0],
                url: ea.url('system.menu/getMenuTips'),
                template_val: '{{d.node}}',
                template_txt: '{{d.node}} <span class=\'layui-badge layui-bg-gray\'>{{d.title}}</span>',
                onselect: function (resp) {
                }
            });

            ea.listen(function (data) {
                return data;
            }, function (res) {
                ea.msg.success(res.msg, function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.$('[data-treetable-refresh]').trigger("click");
                });
            });
        },
        edit: function () {
        var time = 0;
       $(document).on('click','[data-type=tianjia]',function (e){
               var html = "<div class=\"layui-row\">\n" +
                                       "                        <div class=\"layui-form-mid\">键：</div><input class=\"layui-input layui-input-inline\" style=\"width: 100px\" name=\""+field+"[key][]\" value=\"\">\n" +
                                       "                        <div class=\"layui-form-mid\">值：</div><input class=\"layui-input layui-input-inline\" style=\"width: 150px\" name=\""+field+"[value][]\" value=\"\">\n" +
                                       "                        <div class=\"layui-form-mid\"><input type=\"button\" data-type=\"jian\" class=\"layui-btn layui-btn-sm layui-btn-primary layui-border-red\" value=\"-\"></div>\n" +
                                       "                    </div>"
                                   $(this).parent().parent().append(html)
                               })
                                   $(document).on('click','[data-type=jian]',function (e){
                                       console.log(e);
                                       $(this).parent().parent().remove();
                                   })
                    $(".aselect").bind('input propertychange',function (data){
                                            var value = $(this).val();
                                            var url = data.target.dataset.url;
                                            var show_id = data.target.dataset.contentid;
                                            var name = data.target.dataset.name
                                            var table = data.target.dataset.table
                                            var relationship = data.target.dataset.relationship
                                            // console.log(data)
                                            // console.log(show_id)
                                            // console.log(name)
                                            // console.log(table)
                                            // console.log(relationship)
                                            // console.log(url)
                                            // return
                                            time = new Date().getTime();
                                            setTimeout(function (){
                                                var newtime = new Date().getTime();
                                                if(newtime>=time+499){
                                                    $.post(url,{table:table,value:value,name:name,relationship_primary_key:relationship},function (res){
                                                        // console.log(res);
                                                        var select_show = $("#"+show_id);
                                                        select_show.empty();
                                                        if(res.data.length>0){
                                                            var str = '<dd class="xuanzes" data-value="" class="">请选择</dd>';
                                                            $.each(res.data,function (i,v){
                                                                str +='<dd class="xuanzes" data-value="' + v[relationship] + '" class="">' + v[name] + '</dd>';
                                                            })
                                                            select_show.append(str)
                                                            select_show.show();
                                                        }else{
                                                            select_show.hide();
                                                        }
                                                    })
                                                    $('body').on('click','dd',function (data){
                                                        console.log(data);
                                                        var value = data.target.dataset.value;
                                                        var text = data.target.innerText;
                                                        // $(this).parent().prev().children('input')
                                                        // console.log($(this).parent().prev().children('input'))
                                                        $.each($(this).parent().prev().children('input'),function (i,v){
                                                            if(i==1){
                                                                $(v).val(value);
                                                            }else if(i==0){
                                                                $(v).val(text);
                                                            }
                                                        })
                                                        $(this).parent().hide();
                                                    })
                                                }else{
                                                    console.log('时间没到');
                                                }
                                            },500);
                                        })
            iconPickerFa.render({
                elem: '#icon',
                url: PATH_CONFIG.iconLess,
                limit: 12,
                click: function (data) {
                    $('#icon').val('fa ' + data.icon);
                },
                success: function (d) {
                    console.log(d);
                }
            });
            autocomplete.render({
                elem: $('#href')[0],
                url: ea.url('system.menu/getMenuTips'),
                template_val: '{{d.node}}',
                template_txt: '{{d.node}} <span class=\'layui-badge layui-bg-gray\'>{{d.title}}</span>',
                onselect: function (resp) {
                }
            });

            ea.listen(function (data) {
                return data;
            }, function (res) {
                ea.msg.success(res.msg, function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.$('[data-treetable-refresh]').trigger("click");
                });
            });
        },
        details:function(){
                            var time = 0;
                                    $(document).on('click','[data-type=tianjia]',function (e){
                                                    var html = "<div class=\"layui-row\">\n" +
                                                                            "                        <div class=\"layui-form-mid\">键：</div><input class=\"layui-input layui-input-inline\" style=\"width: 100px\" name=\""+field+"[key][]\" value=\"\">\n" +
                                                                            "                        <div class=\"layui-form-mid\">值：</div><input class=\"layui-input layui-input-inline\" style=\"width: 150px\" name=\""+field+"[value][]\" value=\"\">\n" +
                                                                            "                        <div class=\"layui-form-mid\"><input type=\"button\" data-type=\"jian\" class=\"layui-btn layui-btn-sm layui-btn-primary layui-border-red\" value=\"-\"></div>\n" +
                                                                            "                    </div>"
                                                                        $(this).parent().parent().append(html)
                                                                    })
                                                                        $(document).on('click','[data-type=jian]',function (e){
                                                                            console.log(e);
                                                                            $(this).parent().parent().remove();
                                                                        })
                                    $(".aselect").bind('input propertychange',function (data){
                                                            var value = $(this).val();
                                                            var url = data.target.dataset.url;
                                                            var show_id = data.target.dataset.contentid;
                                                            var name = data.target.dataset.name
                                                            var table = data.target.dataset.table
                                                            var relationship = data.target.dataset.relationship
                                                            // console.log(data)
                                                            // console.log(show_id)
                                                            // console.log(name)
                                                            // console.log(table)
                                                            // console.log(relationship)
                                                            // console.log(url)
                                                            // return
                                                            time = new Date().getTime();
                                                            setTimeout(function (){
                                                                var newtime = new Date().getTime();
                                                                if(newtime>=time+499){
                                                                    $.post(url,{table:table,value:value,name:name,relationship_primary_key:relationship},function (res){
                                                                        // console.log(res);
                                                                        var select_show = $("#"+show_id);
                                                                        select_show.empty();
                                                                        if(res.data.length>0){
                                                                            var str = '<dd class="xuanzes" data-value="" class="">请选择</dd>';
                                                                            $.each(res.data,function (i,v){
                                                                                str +='<dd class="xuanzes" data-value="' + v[relationship] + '" class="">' + v[name] + '</dd>';
                                                                            })
                                                                            select_show.append(str)
                                                                            select_show.show();
                                                                        }else{
                                                                            select_show.hide();
                                                                        }
                                                                    })
                                                                    $('body').on('click','dd',function (data){
                                                                        console.log(data);
                                                                        var value = data.target.dataset.value;
                                                                        var text = data.target.innerText;
                                                                        // $(this).parent().prev().children('input')
                                                                        // console.log($(this).parent().prev().children('input'))
                                                                        $.each($(this).parent().prev().children('input'),function (i,v){
                                                                            if(i==1){
                                                                                $(v).val(value);
                                                                            }else if(i==0){
                                                                                $(v).val(text);
                                                                            }
                                                                        })
                                                                        $(this).parent().hide();
                                                                    })
                                                                }else{
                                                                    console.log('时间没到');
                                                                }
                                                            },500);
                                                        })
                            $(".icon").each(function (i,v) {
                                var ids = $(v).attr('id');
                                var myiconPickerFa = iconPickerFa.render({
                                    elem:"#"+ids,
                                    url: PATH_CONFIG.iconLess,
                                    limit: 12,
                                    click: function (data) {
                                        $('#'+ids).val('fa ' + data.icon);
                                    },
                                    success: function (d) {
                                        console.log(d);
                                    }
                                })
                            })
                            $(".color").each(function (i,v) {
                                var ids = $(v).attr('id');
                                var coles = $('[name='+ids+']').val();
                                //初始色值
                               var mycolorpicker = colorpicker.render({
                                    elem: '#'+ids
                                    ,color: coles //设置默认色
                                    ,change:function (color) {
                                       $('[name='+ids+']').val(color);
                                   }
                                });
                            })
                            $(".datetime").each(function (i,v) {
                                var ids = $(v).attr('id');
                                laydate.render({
                                    elem: '#'+ids
                                    ,type: 'datetime'
                                });
                            })
                            ea.listen(function (data) {
                            console.log(data)
                                return data;
                            }, function (res) {
                                console.log(res);
                                ea.msg.success(res.msg, function () {
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);
                                    parent.$('[data-treetable-refresh]').trigger("click");
                                });
                            });
                }
    };
    return Controller;
});